/**
 * 自定义ajax方法
 */
function ajaxGet(url, callback, defaultResult) {
    /* var target = event.target;
     target.disabled = true;
     var oval = target.value;
     target.value = "提交中....";
     if (defaultResult == undefined) {
     defaultResult = {"msg": "提交成功.", "result": true};
     }*/

    $.get(url, callback);
}

/**
 *绑定/修改报关单号
 */
function doEditSNO(id, defaultResult) {
    ajaxLoader.ajaxPost('../datalock/lock?opaId=VDBS&bidId=' + id, {}, function (data) {
        if ("200" == data.errcode) {
            var target = event.target;
            defaultResult =
                (defaultResult == "null" || defaultResult == "undefined") ? "" : defaultResult;
            var sno = prompt("请输入报关单号", defaultResult ? defaultResult : "");
            if (sno) {
                ajaxGet("doEditSNO?id=" + id + "&sno=" + sno, function (result) {
                    jw.alert(result.msg, {
                        onOk: function () {
                            if (result.result) {
                                target.innerHTML = sno;
                            }
                            else {
                                //TODO
                            }
                            ajaxLoader.ajaxPost('../datalock/onUnlock?opaId=VDBS&bidId=' + id);
                        }
                    });
                });
            } else {
                ajaxLoader.ajaxPost('../datalock/onUnlock?opaId=VDBS&bidId=' + id);
            }
        } else {
            jw.alert(data.errmsg);
        }
    });
}

/**
 * 打开修改页面
 * @param id
 * @param declaration_type
 */
function doEditSingle(id, declaration_type,pageType, editAble, flag) {
    if (!id) {
        alert("打开页面错误，请与管理员联系！");
        return;
    }
    ajaxLoader.ajaxPost('../datalock/lock?opaId=VDBS&bidId=' + id, {}, function (data) {
        if (!data) {
            jw.alert(data);
        }
        if ("200" == data.errcode) {
            if (editAble && editAble == true) {
                if (pageType == "CROPPED5") {
                    window.location.href = "../business/agent/order/update?id=" + id;
                }else{
                    window.location.href = "../order/update?id=" + id;
                }
            } else {
                if (pageType == "CROPPED5") {
                    window.location.href = "../business/agent/order/detail?id=" + id;
                }else{
                    window.location.href = "../order/detail?id=" + id;
                }
            }
        } else {
            jw.alert(data.errmsg);
        }
    });
}

/**
 * 打开修改页面
 * @param id
 * @param declaration_type
 */
function doEdit(id, declaration_type,pageType, editAble,flag) {
    if (!id) {
        alert("打开页面错误，请与管理员联系！");
        return;
    }
    if (editAble && editAble == true) {
        if (pageType == "CROPPED5") {
            window.location.href = "../business/agent/order/update?id=" + id;
        }else{
            window.location.href = "../order/update?id=" + id;
        }
    } else {
        if (pageType == "CROPPED5") {
            window.location.href = "../business/agent/order/detail?id=" + id;
        }else{
            window.location.href = "../order/detail?id=" + id;
        }
    }
}

/**
 * 根据ID删除单证
 * @param id
 */
function doDel(id) {
    if (!id) {
        alert("删除单证失败，请联系管理员！");
        return;
    }
    $.ajax({
               url: "./deleteDeclaration",
               type: "POST",
               data: "tdateaa=" + timeNum() + "&id=" + id,
               success: function (data) {
                   alert(data.msg);
               }
           });
}

/**
 * 底单下载
 */
function doDownload(id, type) {
    if (!id) {
        alert("底单下载失败，请联系管理员！");
        return;
    }
    jw.dialog({
                  iframe: "./downloadPage?type=" + type + "&attaId=" + id,
                  iframeFetchTitle: false,
                  width: 800,
                  height: 350
              });
}

/**
 * 查看评价详细页面
 * @param relationId
 */
function lookEvaluation(relationId) {
    if (!relationId) {
        alert("打开失败，请联系管理员！");
        return;
    }
    jw.dialog({
                  iframe: "../../evaluation/view?&relationId=" + relationId,
                  width: 800,
                  height: 350
              });
}

/**
 * 评价页面
 * @param relationId
 */
function doEvaluate(relationId) {
    if (!relationId) {
        alert("打开失败，请联系管理员！");
        return;
    }
    jw.dialog({
                  iframe: "../../evaluation/edit?&relationId=" + relationId,
                  width: 800,
                  height: 350
              });
}

/**
 * 查验货物更新页面
 * @param declarationId
 * @param inspectionId
 */
function inspectionUpdatePage(declarationId, inspectionId) {
    if (!inspectionId) {
        alert("打开页面错误，请与管理员联系！");
        return;
    }
    /*window.location.href =
     "/inspectionGoods/updatePage?declarationId=" + declarationId + "&inspectionId="
     + inspectionId;*/
    window.location.href =
        "/views/yxtPc/pages/tarce.html?trackId=" + inspectionId;
}

/**
 * 查验货物查看页面
 * @param declarationId
 * @param inspectionId
 */
function inspectionViewPage(declarationId, inspectionId) {
    if (!declarationId) {
        alert("打开页面错误，请与管理员联系！");
        return;
    }
    window.location.href =
        "/inspectionGoods/viewPage?declarationId=" + declarationId + "&inspectionId="
        + inspectionId;
}

/**
 *供应商-报关单标识放行
 */
function doDeclarationOk(id, okType) {
    jw.confirm("确认完成" + okType + "?", function () {
        var url = "doDeclarationOk?id=" + id + "&okType=" + okType;
        $.get(url, function (result) {
            jw.alert(result.msg);
            $("#declarationsDataTable").mrdForm("refreshGrid");
        });
    });
}

/**
 * 报关单标识放行
 * @param declarationId
 * @param okType
 */
function doDeclarationOk(declarationId, okType) {
    if (!declarationId || !okType) {
        alert("提交错误，请联系管理员！");
        return;
    }
    ajaxGet("../business/doDeclarationOk?id=" + declarationId + "&okType=" + okType,
        function (result) {
            jw.alert(result.msg);
            $("#declarationsDataTable").mrdForm("refreshGrid");
        });
}

/**
 * 展示联系方式
 */
function showContactWay(did, role) {
    $.ajax({
               url: "/business/getContactWay",
               cache: false,
               dataType: 'json',
               data: {"did": did, "role": role},
               success: function (data) {
                   if (data.result) {
                       var user = data.data;
                       var phone = user.uMobile;
                       if (!phone) {
                           phone = '暂无';
                       }
                       jw.alert("联系人：" + user.uName + "<br/>联系电话：" + phone);
                   } else {
                       jw.alert(data.msg);
                   }
               }
           });
}
